// `include    "top_define.v"
//crc域计算
//10.08.25
module multi_hash(
					clk         ,
					rst_n         ,
					multi_mac       ,
					multi_mac_hash_en ,
					multi_hash_en     ,
					multi_hash_mac ,
					hash_busy
					);
					
					
input        clk         ;
input        rst_n         ;
input        [23:0]multi_mac    ;
input        multi_mac_hash_en ;
output       multi_hash_en     ;
output       hash_busy;                        ///正在工作，忙状态

output       [9:0] multi_hash_mac ;



reg   [9:0]  crc_reg ;

reg   [9:0]  multi_hash_mac;
reg          multi_hash_en;
reg   [2:0]  state_machine;

reg   [23:0]  multi_mac_reg;
reg	  hash_busy;


     parameter  idle    = 3'b001,
                state_1 = 3'b010,
                state_2 = 3'b100;

/**************************************CRC8并行计算，数据位宽16***********************************************************************/
  // polynomial: (0 1 2 8)
  // data width: 8
  // convention: the first_n serial bit is D[7]
  function [9:0] CRC48_D10;

    input [47:0] Data;
    input [9:0] crc;
    reg [47:0] d;
    reg [9:0] c;
    reg [9:0] newcrc;
  begin
    d = Data;
    c = crc;

    newcrc[9]  = d[47]^d[46]^d[44]^d[43]^d[42]^d[41]^d[40]^d[38]^d[37]^d[34]^d[33]^d[29]^d[27]^d[26]^d[25]^d[19]^d[14]^d[13]^d[12]^d[11]^d[10]^d[0]^c[8]^c[6]^c[5]^c[1];
    newcrc[8]  = d[46]^d[45]^d[40]^d[39]^d[37]^d[35]^d[33]^d[30]^d[29]^d[28]^d[25]^d[20]^d[19]^d[15]^d[1]^c[9]^c[8]^c[7]^c[6]^c[4]^c[1]^c[0];
    newcrc[7]  = d[47]^d[46]^d[41]^d[40]^d[38]^d[36]^d[34]^d[31]^d[30]^d[29]^d[26]^d[21]^d[20]^d[16]^d[11]^d[2]^c[8]^c[7]^c[6]^c[5]^c[3]^c[0];
    newcrc[6]  = d[47]^d[42]^d[41]^d[39]^d[37]^d[35]^d[32]^d[31]^d[30]^d[27]^d[22]^d[21]^d[17]^d[12]^d[3]^c[9]^c[7]^c[6]^c[5]^c[4]^c[2];
    newcrc[5]  = d[47]^d[46]^d[44]^d[41]^d[37]^d[36]^d[34]^d[32]^d[31]^d[29]^d[28]^d[27]^d[26]^d[25]^d[23]^d[22]^d[19]^d[18]^d[14]^d[12]^d[11]^d[10]^d[4]^c[9]^c[4]^c[3]; 
    newcrc[4]  = d[46]^d[45]^d[44]^d[43]^d[41]^d[40]^d[35]^d[34]^d[32]^d[30]^d[28]^d[25]^d[24]^d[23]^d[20]^d[15]^d[14]^d[10]^d[5]^c[9]^c[6]^c[5]^c[3]^c[2]^c[1];
    newcrc[3]  = d[47]^d[46]^d[45]^d[44]^d[42]^d[41]^d[36]^d[35]^d[33]^d[31]^d[29]^d[26]^d[25]^d[24]^d[21]^d[16]^d[15]^d[11]^d[6]^c[8]^c[5]^c[4]^c[2]^c[1]^c[0];
    newcrc[2]  = d[47]^d[46]^d[45]^d[43]^d[42]^d[37]^d[36]^d[34]^d[32]^d[30]^d[27]^d[26]^d[25]^d[22]^d[17]^d[16]^d[12]^d[7]^c[9]^c[7]^c[4]^c[3]^c[1]^c[0];
    newcrc[1]  = d[47]^d[46]^d[44]^d[43]^d[38]^d[37]^d[35]^d[33]^d[31]^d[28]^d[27]^d[26]^d[23]^d[18]^d[17]^d[13]^d[8]^c[9]^c[8]^c[6]^c[3]^c[2]^c[0];
    newcrc[0]  = d[46]^d[45]^d[43]^d[42]^d[41]^d[40]^d[39]^d[37]^d[36]^d[33]^d[32]^d[28]^d[26]^d[25]^d[24]^d[18]^d[13]^d[12]^d[11]^d[10]^d[9]^c[9]^c[7]^c[6]^c[2];
    CRC48_D10 = newcrc;
  end
  endfunction


always @(posedge clk or negedge rst_n)
    if(~rst_n)
      begin
	    crc_reg     <= 10'h3ff;
	    multi_hash_mac <= 10'b0;
	    multi_hash_en <= 1'b0;
	    hash_busy     <= 1'b0;
	  end
	else if(multi_mac_hash_en)
      begin
        crc_reg <= 10'h3ff;
        multi_hash_mac <= CRC48_D10({multi_mac,multi_mac},crc_reg);
        hash_busy <= 1'b1;
        multi_hash_en <= 1'b1;
	  end
    else 
      begin
        crc_reg <= 10'h3ff;
        multi_hash_mac <= 10'h0;
        hash_busy <= 1'b0;
        multi_hash_en <= 1'b0;  
      end


endmodule
